//
//  WBNewFeatureView.swift
//  WB
//
//  Created by macssss on 16/7/6.
//  Copyright © 2016年 macssss. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {
    
    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    //进入微博
    @IBAction func enterstaus() {
        
        removeFromSuperview()
    }
    
       
    
    class func newFeatuerView() -> WBNewFeatureView {
        
        //从XIB加载视图
        let nib = UINib(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView

        v.frame = UIScreen.main().bounds
        
        return v
        
    }
    
    
    //布局新特性界面
    override func awakeFromNib() {
        
        let coutn = 4
        let rect = UIScreen.main().bounds
        
        //循环添加图片
        for i in 0..<coutn {
            
            let imgeName = "new_feature_\(i + 1)"
            let iv = UIImageView(image: UIImage(named: imgeName))
            
            //设置大小
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(iv)
        }
        
        //设置scrollView的属性
        scrollView.contentSize = CGSize(width: CGFloat(coutn + 1) * rect.width, height: rect.height)
        
       // 关闭scrollView 的弹力等属性
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        
        enterButton.isHidden = true
        
        scrollView.delegate = self
        
        
    }
 
}


//MARK: - scrollView 代理方法
extension WBNewFeatureView: UIScrollViewDelegate {
    
    //did 滚动结束调用
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == scrollView.subviews.count {
            
            print("欢迎欢迎")
            removeFromSuperview()
            
        }
        
        enterButton.isHidden = (page != scrollView.subviews.count - 1)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //一滚动就影藏阿牛
        enterButton.isHidden = true
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pageControl.currentPage = page
        
        pageControl.isHidden = (page == scrollView.subviews.count)
    }
}
















